package com.credit.utlis;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;


public class ImgUpload {
	
	public static String PictureUpload(MultipartFile photo,HttpServletRequest request) throws Exception, IOException{
		
		String pathRoot=request.getSession().getServletContext().getRealPath("/");
		//把图片放到项目外面,下次部署不会被覆盖
		pathRoot = pathRoot.substring(0, 43)+File.separator+"/IMG/credit_back/";
		System.out.println(pathRoot);
		
		/*String pathRoot=request.getSession().getServletContext().getRealPath("images"+File.separator+"picture");
		System.out.println(pathRoot);*/
		String fileName="";
		String url="";
		if(!photo.isEmpty()){
			String uuid = UUID.randomUUID().toString().replaceAll("-", "");
			String contentType = photo.getContentType();
			String suffixName = contentType.substring(contentType.indexOf("/") + 1);
			fileName=uuid+"."+suffixName;
			File targetFile=new File(pathRoot, fileName);
			if(!targetFile.exists()){
				targetFile.mkdirs();
			}
			photo.transferTo(targetFile);
			InputStream resourceAsStream = ImgUpload.class.getClassLoader().getResourceAsStream("propertiesUtil.properties");
			Properties properties = new Properties();
			properties.load(resourceAsStream);
			//String prefix = properties.getProperty("local.imgPath.prefix").toString();//本地
			String prefix = properties.getProperty("server.imgPath.prefix").toString();//服务器

			url = prefix+fileName;
		}else{
			url=null;
		}
		
		return url;
	}
	//生成二维码传到服务器
	 public static String LoadImageToServer(String filePath,
			 String serverPath) throws Exception { 

		    String resultPath = "";          //上传后图片所在的路径 
		    FileOutputStream out = null;     //文件输出流 
		    try {                               //验证图片上传的格式是否正确 
		     File f = new File(filePath); 
		        if (!f.isFile()) { 
		        throw new Exception(f+ " 不是图片文件!"); 
		    } 
		     if (f != null && f.exists()) {          //这里的ImageIO属于java工厂类，在工厂类class里面，调用的System.gc()，频繁调用会造成dump，需要考虑优化 
		        BufferedImage image = ImageIO.read(f); // 读入文件 
		        if (image != null) { 
			        BufferedImage tag = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);  //构造一个类型为预定义图像类型之一的 BufferedImage
			           tag.getGraphics().drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);                     //绘制所需要尺寸大小的图片 
			        /* 
			         * 以下生成图片上传后在服务器上的新路径 
			         */ 
			        int lastLength = filePath.lastIndexOf("."); 
			        
			        String imageName = Tools.formatDateTime(new Date(), "QRlong");                         //以系统时间来随机的创建图片文件名 
			        String fileType = filePath.substring(lastLength);              //获取上传图片的类型 
			        resultPath = serverPath+ imageName +fileType; 
			       
		      } 
		     } 

		     f = null; 

		    } catch (Exception ex) { 
		     ex.printStackTrace(); 
		    } finally { 
		   
		     out = null; 
		    } 
		    return resultPath; 
		   } 

}
